package com.smile.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smile.commons.page.MyBatisPlusPageConverter;
import com.smile.commons.page.R;
import com.smile.model.dto.request.*;
import com.smile.model.response.response.GetDroneByIdResponseDTO;
import com.smile.model.response.response.GetDroneListResponseDTO;
import com.smile.model.response.response.GetNoBindDroneResponseDTO;
import com.smile.model.request.*;
import com.smile.model.response.GetDroneByIdResponse;
import com.smile.model.response.GetDroneListResponse;
import com.smile.model.response.GetNoBindDroneResponse;
import com.smile.service.DroneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;

@RestController
@RequestMapping("/drone")
public class DroneController {

    @Autowired
    private DroneService droneService;

//    @Autowired
//    private DeviceClient deviceClient;
//
//    @PostMapping("/test/topic")
//    public R test(){
//        return R.ok();
//    }

    @PostMapping(value = "/addDrone")
    public R addDrone(@Valid @RequestBody AddDroneRequest addDroneRequest) throws IOException {
        AddDroneRequestDTO addDroneRequestDTO = new AddDroneRequestDTO();
        BeanUtil.copyProperties(addDroneRequest, addDroneRequestDTO);
        Boolean result = droneService.addDrone(addDroneRequestDTO);
        if (result) {
            return R.ok();
        } else {
            return R.fail();
        }
    }


    @PostMapping("/getNoBindDrone")
    public R<Page<GetNoBindDroneResponse>> getNoBindDrone(GetNoBindDroneRequest getNoBindDroneRequest) {
        GetNoBindDroneRequestDTO getNoBindDroneRequestDTO = BeanUtil.copyProperties(getNoBindDroneRequest, GetNoBindDroneRequestDTO.class);
        Page<GetNoBindDroneResponseDTO> getNoBindDroneResponseDTO = droneService.getNoBindDrone(getNoBindDroneRequestDTO);
        return R.ok(MyBatisPlusPageConverter.convertPage(getNoBindDroneResponseDTO, GetNoBindDroneResponse.class));
    }

//    @PostMapping("/edit")

    @PostMapping("/editDrone")
    public R editDrone(@Valid @RequestBody EditDroneRequest editDroneRequest, HttpServletRequest request) throws IOException {
        EditDroneRequestDTO editDroneRequestDTO = new EditDroneRequestDTO();
        BeanUtil.copyProperties(editDroneRequest, editDroneRequestDTO);
        Boolean result = droneService.editDrone(editDroneRequestDTO);
        if (result) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @PostMapping("/getDroneList")
    public R<Page<GetDroneListResponse>> getDroneList(@RequestBody GetDroneListRequest getDroneListRequest) throws IOException {
        GetDroneListRequestDTO getDroneListRequestDTO = BeanUtil.copyProperties(getDroneListRequest, GetDroneListRequestDTO.class);
        Page<GetDroneListResponseDTO> pages = droneService.getDroneList(getDroneListRequestDTO);

        return R.ok(MyBatisPlusPageConverter.convertPage(pages, GetDroneListResponse.class));
    }

    @PostMapping("/getDroneById")
    public R<GetDroneByIdResponse> getDroneById(@RequestBody GetDroneByIdRequest getDroneByIdRequest) {
        GetDroneByIdRequestDto getDroneByIdRequestDto = BeanUtil.copyProperties(getDroneByIdRequest, GetDroneByIdRequestDto.class);
        GetDroneByIdResponseDTO getDroneByIdResponseDTO = droneService.getDroneById(getDroneByIdRequestDto);
        return R.ok(BeanUtil.copyProperties(getDroneByIdResponseDTO, GetDroneByIdResponse.class));
    }

    @PostMapping("/deleteDrone")
    public R<Void> deleteDrone(@RequestBody Long id) {
        Boolean result = droneService.deleteDrone(id);
        if (result) {
            return R.ok();

        } else {
            return R.fail();
        }

    }

}
